package utils

import (
	"errors"
	"reflect"
)

// ValidateValueTyp val需要时指向结构体的指针
func ValidateValueTyp(val any) error {
	typ := reflect.TypeOf(val)
	if typ.Kind() != reflect.Pointer || typ.Elem().Kind() != reflect.Struct {
		return errors.New("val must be a pointer to struct")
	}
	return nil
}

// ValidateValuesTyp vals需要是指向切片的指针,切片元素需要是指向结构体的指针,示例: userList := make([]*User, 0),需要传入&userList
func ValidateValuesTyp(vals any) (valTyp reflect.Type, err error) {
	typ := reflect.TypeOf(vals)
	if typ.Kind() != reflect.Pointer || typ.Elem().Kind() != reflect.Slice {
		return nil, errors.New("vals must be a pointer to slice")
	}
	typ = typ.Elem()    // 切片
	valTyp = typ.Elem() // 切片中的元素类型
	if valTyp.Kind() != reflect.Pointer || valTyp.Elem().Kind() != reflect.Struct {
		return nil, errors.New("val must be a pointer to struct")
	}
	valTyp = valTyp.Elem()

	return valTyp, nil
}
